import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { viteMockServe } from "vite-plugin-mock";
import { fileURLToPath } from "url";
import AutoImport from "unplugin-auto-import/vite";
import Icons from "unplugin-icons/vite";
import Components from "unplugin-vue-components/vite";
import IconsResolver from "unplugin-icons/resolver";
import ElementPlus from "unplugin-element-plus/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

// https://vitejs.dev/config/
export default defineConfig({
  css: {
    preprocessorOptions: {
      less: {
        additionalData: `@import '@/styles/variable.less';`,
      },
    },
  },
  plugins: [
    vue(),
    viteMockServe({
      mockPath: "mock",
      enable: true,
    }),
    // 开启ElementPlus自动引入CSS
    ElementPlus({}),
    // 自动安装图标
    Icons({ autoInstall: true }),
    // 自动引入组件及ICON
    AutoImport({
      imports: ["vue", "vue-router", "pinia"],
      eslintrc: {
        enabled: true,
        filepath: fileURLToPath(new URL(".eslintrc-auto-import.mjs", import.meta.url)),
      },
      resolvers: [IconsResolver(), ElementPlusResolver()],
      dts: fileURLToPath(new URL("./types/auto-imports.d.ts", import.meta.url)),
    }),
    // 自动注册组件
    Components({
      resolvers: [
        IconsResolver({
          prefix: "Icon",
          customCollections: ["ep"],
        }),
        ElementPlusResolver(),
      ],
      dts: fileURLToPath(new URL("./types/components.d.ts", import.meta.url)),
    }),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
      "#": fileURLToPath(new URL("./types", import.meta.url)),
    },
  },
  server: {
    open: true,
  },
});
